/*
 * @Author: sympsel sympsel@outlook.com
 * @Date: 2025-07-14 15:44:50
 * @LastEditors: sympsel sympsel@outlook.com
 * @LastEditTime: 2025-07-14 19:41:20
 * @FilePath: \work25_7_14\src\SeqList.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * 
 */

#include "SeqList.h"
// 对数据的管理:增删查改 
SeqList* SeqListInit() {
    SeqList* list = MALLOC(SeqList, 1);
    list->size = 0;
    list->capacity = 10;
    list->data = MALLOC(SLDateType, DEFAULT_CAPACITY);
    return list;
}

void SeqListDestroy(SeqList* list);

void SeqListPrint(SeqList* list) {
    printf("size=%d, capacity=%d\n", list->size, list->capacity);
    printf("[");
    for (int i = 0; i < list->size; ++i) {
        printf(i == list->size - 1 ? "%d" : "%d, ", list->data[i]);
    }
    printf("]\n");
}

bool IsFull(SeqList* list) {
    return list->size >= list->capacity;
}

bool IsEmpty(SeqList* list) {
    return list->size == 0;
}

void SeqListPushBack(SeqList* list, SLDateType data) {
    if (IsFull(list)) {
        list->capacity += list->capacity / 2;
    }
    list->data[list->size++] = data;
}

void SeqListPushFront(SeqList* list, SLDateType data) {
    if (IsFull(list)) {
        list->capacity += list->capacity / 2;
    }
    for (int i = list->size; i > 0; --i) {
        list->data[i] = list->data[i - 1];
    }
    list->data[0] = data;
    list->size++;
}

void SeqListPopFront(SeqList* list) {
    if (!IsEmpty(list)) {
        for (int i = 0; i < list->size - 1; ++i) {
            list->data[i] = list->data[i + 1];
        }
        list->size--;
    }
}

void SeqListPopBack(SeqList* list) {
    if (!IsEmpty(list)) {
        list->size--;
    }
}

// 顺序表查找
bool SeqListFind(SeqList* list, SLDateType data) {
    for (int i = 0; i < list->size - 1; ++i) {
        if (list->data[i] == data) {
            printf("Find %d at pos %d, index %d!\n", list->data[i], i + 1, i);
            return true;
        }
    }
    printf("Not found %d!\n", data);
}
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* list, int pos, SLDateType data) {
    assert(pos <= list->size && pos >= 1);

    if (IsFull(list)) {
        list->capacity += list->capacity / 2;
    }
    for (int i = list->size; i > pos; --i) {
        list->data[i] = list->data[i - 1];
    }
    list->data[pos - 1] = data;
}
// 顺序表删除pos位置的值
void SeqListErase(SeqList* list, int pos) {
    if (!IsEmpty(list)) {
        assert(pos <= list->size && pos >= 1);
        for (int i = pos - 1; i < list->size - 1 - 1; ++i) {
            list->data[i] =  list->data[i + 1];
        }
        list->size--;
    }
}